package com.iwjw.ibatis.entity;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.session.RowBounds;

import java.util.List;

/**
 * 分页工具类
 *
 * @author: huangweiwei
 * @email: ihuangweiwei@163.com
 * @created:2016/10/31
 */
@Data
@NoArgsConstructor
public class Page<T> extends RowBounds {

    private Integer total; //总条数
    private Integer page;//页数
    private Integer pageSize;//每页条数
    private List<T> data; //分页数据
    private Integer allPage;//总页数

    private Boolean useFlag = Boolean.TRUE;

    /**
     * 构建查询语句
     *
     * @param page
     * @param pageSize
     */
    public Page(Integer page, Integer pageSize) {
        if (page == null || page <= 0) {
            page = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        this.page = page;
        this.pageSize = pageSize;
    }


    /**
     * 构建结果封装
     *
     * @param total
     * @param page
     * @param pageSize
     */
    public Page(Integer total, Integer page, Integer pageSize) {
        if (total <= 0) {
            this.total = 0;
            this.page = 1;
            this.pageSize = pageSize;
            this.allPage = 0;
        } else {
            this.total = total;
            this.page = page;
            this.pageSize = pageSize;
            this.allPage = (int) ((total + pageSize - 1) / pageSize);
        }
    }


    public void setTotal(Integer total) {
        this.total = total;
    }

    public Boolean isEnd() {
        if (allPage != null && this.page != null) {
            return allPage.equals(page);
        }
        return false;
    }
}
